package com.example.demo.common;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-07
 * Time: 20:01
 */

import lombok.Data;

import java.io.Serializable;


/**
 * 统一的数据的返回类型
 */
@Data
public class AjaxResult implements Serializable {  //实现反/序列化接口
    private int code;//状态码
    private String message;//状态码的描述
    private Object data;//返回的数据条数

    /**
     * 请求处理成功返回的格式
     */
    public static AjaxResult success(Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(200);
        result.setData(data);
        result.setMessage("");
        return result;
    }

    public static AjaxResult success(int code, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setData(data);
        result.setMessage("");
        return result;
    }

    public static AjaxResult success(int code, String message, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    /**
     * 请求处理失败返回的格式
     */
    public static AjaxResult fail(int code, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setMessage(null);
        result.setData(data);
        return result;
    }

    public static AjaxResult fail(int code, String message, Object data) {
        AjaxResult result = new AjaxResult();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
}
